<?php

namespace App\Http\Controllers\Kms\App\Common;

use App\Http\Controllers\BaseController;
use App\Http\Validate\Kms\Common\ConfigValidate;
use App\Kuafu\Config\Enum\ConfigCodeEnum;
use App\Kuafu\Config\Enum\ConfigEnum;
use App\Models\Kms\KmsIosCode;
use App\Service\Kms\Common\ConfigService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class ConfigController extends BaseController
{
    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function noLogin(Request $request): JsonResponse
    {
        $params = ConfigValidate::noLoginValidate($request->merge(["version" => $request->header("version")])->all());
        return self::success(ConfigService::noLogin($params));
    }


    /**
     * 获取夸父运营版本强制更新信息
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     */
    public function getAppVersionInfo(Request $request): JsonResponse
    {
        $this->validate(
            $request, [
                'appType'     => 'required|string|in:' . implode(',', array_keys(ConfigService::APP_TYPE)),
                'nowVersion'  => 'required|string',
                'appNameType' => 'required|int|in:' . implode(',', array_keys(ConfigService::APP_NAME_TYPE_MAP)),
            ]
        );
        return self::success(ConfigService::getAppVersionInfo($request->appType, $request->nowVersion, $request->appNameType));
    }


    /**
     * 夸父运营ios code使用记录
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     */
    public function iosCodeUse(Request $request): JsonResponse
    {
        $this->validate(
            $request, [
                'code'   => 'required|string',
                'status' => 'required|int|in:' . implode(',', array_keys(KmsIosCode::STATUS_MAP_TEXT)),
            ]
        );
        return self::success(ConfigService::iosCodeUse($request->code));
    }

    /**
     * 夸父运营ios code
     * @param Request $request
     * @return JsonResponse
     */
    public function getIosCode(Request $request): JsonResponse
    {
        return self::success(ConfigService::getIosCode());
    }
}
